clear all
set more off
set mem 10000000
set matsize 10000

*****************************************
*** Build NSS data and merge together ***
*****************************************

** Set file paths
do "$path_code/paths.do"

********************************************************************************
********************************************************************************

** Step 1: Read in and clean the multiplier files
{
# delim ;	
qui infile using "$nss/raw/multipliers/Dictionaries/mlt_61_1.dic" ;	save "$nss/Data/Final/mlt_61_1.dta", replace; clear;

qui infile using "$nss/raw/multipliers/Dictionaries/mlt_66_1.dic" ;	save "$nss/Data/Final/mlt_66_1.dta", replace; clear;
# delim cr		
}

********************************************************************************
********************************************************************************

** Step 2: 1999-2000 wave
{
	
** Read in and clean the 1999-2000 data; grab covariates of interest
use "$nss/raw/55th_round_1999_00/Data/Blocks 1,3,12_Household Characteristics.dta", clear  

rename *, lower

keep hhid id roundschedule sector state_region state stratum district subround subsample vill_blk_slno visitno segmentno ///
  stage2_stratum hhold_no survey_code substn_code nss nsc mult ss_replicate mpce30 mpce7 ///
  tmc b3_q17 b3_q18 b3_q1 tmcnv mpc_code_r_u mpc_code_combined wgt_subsample wgt_combined

rename b3_q1 household_size
rename b3_q17 cooking_code
rename b3_q18 lighting_code


gen electric_cooking = 0
replace electric_cooking = 1 if cooking_code == "08"
gen electric_lighting = 0
replace electric_lighting = 1 if lighting_code == "5"
save "$nss/temp/temp_99_00_hh_chars.dta", replace


use "$nss/raw/55th_round_1999_00/Data/Block 5pt1_Monthly household expenditure on fuel and light.dta" , clear
rename *, lower

keep hhid id roundschedule sector state_region state stratum district subround subsample vill_blk_slno visitno segmentno ///
  stage2_stratum hhold_no nss nsc mult ss_replicate b5_1_q1 b5_1_q3 b5_1_q4 wgt_subsample wgt_combined

gen fuellight_value = b5_1_q4 if b5_1_q1 == "359"
label var fuellight_value "tot expenditure on fuel and light (30 days)"

gen electricity_quantity = b5_1_q3 if b5_1_q1 == "342"
gen electricity_value = b5_1_q4 if b5_1_q1 == "342"

gen other_fuel_value = b5_1_q4 if b5_1_q1 == "353"

collapse(max) fuellight_value electricity_quantity electricity_value other_fuel_value, by (hhid id roundschedule sector state_region state stratum district subround subsample vill_blk_slno visitno segmentno ///
  stage2_stratum hhold_no nss nsc mult ss_replicate wgt_subsample wgt_combined)

assert fuellight >= electricity_value if fuellight !=. & electricity_value !=.
replace fuellight = 0 if fuellight == .
replace electricity_value = 0 if electricity_value == .
replace electricity_quant = 0 if electricity_quant == .
replace other_fuel_value = 0 if other_fuel_value == .

label var fuellight "Total spending on fuel and light (Rs)"
label var electricity_value "Total spending on electricity (Rs)"
label var electricity_quantity "Total quantity of electricity (std units)"
label var other_fuel_value "Total spending on other fuel (Rs)"
  
gen implied_elec_price = electricity_val / electricity_quant 
label var implied_elec_price "Electricity price [Value (Rs) / quantity (units)]" 

save "$nss/temp/temp_99_00_fuelspending.dta", replace

use "$nss/raw/55th_round_1999_00/Data/Block 9_Annual household expenditure on durable goods.dta", clear
rename *, lower
keep hhid id roundschedule sector state_region state stratum district subround subsample vill_blk_slno visitno segmentno ///
  stage2_stratum hhold_no nss nsc mult ss_replicate b9_q1 no_of_durables_on_use vl30 wgt_subsample wgt_combined

gen record_player_no = no_of_durables if b9_q1 == "560"
gen radio_no = no_of_durables if b9_q1 == "561"
gen tv_no = no_of_durables if b9_q1 == "562"
gen vcr_no = no_of_durables if b9_q1 == "563"

gen fan_no = no_of_durables if b9_q1 == "590"
gen ac_no = no_of_durables if b9_q1 == "591"
gen sewing_machine_no = no_of_durables if b9_q1 == "593"
gen washing_machine_no = no_of_durables if b9_q1 == "594"
gen fridge_no = no_of_durables if b9_q1 == "597"
gen electric_heating_appliance_no = no_of_durables if b9_q1 == "598"

gen elec_heating_appliance_value = vl30 if b9_q1 == "598"


collapse(max) record radio tv vcr fan ac sewing washing fridge electric elec_, by(hhid id roundschedule sector state_region state stratum district subround subsample vill_blk_slno visitno segmentno ///
  stage2_stratum hhold_no nss nsc mult ss_replicate  wgt_subsample wgt_combined)

foreach var of varlist record - elec_ {
  replace `var' = 0 if `var' == .
}  
save "$nss/temp/temp_99_00_elecdurables.dta", replace
 

** Merge 1990-00 datasets together
use "$nss/temp/temp_99_00_hh_chars.dta", clear
merge 1:1 hhid using "$nss/temp/temp_99_00_fuelspending.dta", keep(1 3) nogen
merge 1:1 hhid using "$nss/temp/temp_99_00_elecdurables.dta", keep(1 3) nogen

foreach var of varlist electric_cooking - electricity_value other_fuel_value record_player - elec_heating {
  replace `var' = 0 if `var' == .
}
save "$nss/temp/temp_99_00_allelecdata.dta", replace

}

********************************************************************************
********************************************************************************

** Step 3: 2004-2005 wave
{

** Read in and clean the 2004-2005 data; grab covariates of interest
use "$nss/raw/61st_round_2004_05/Data/Blocks 1,2 & 12_ Identification of sample household and perception of household regarding sufficiency of food.dta", clear  
rename *, lower
keep hhid-substn_code nss nsc mlt wgt_subsample wgt_combined 
save "$nss/temp/temp_04_05_hh_chars.dta", replace

use "$nss/raw/61st_round_2004_05/Data/Block 3 Part 1_Household Characteristics.dta", clear
rename *, lower
keep hhid-lvl nss nsc mlt wgt_* b3_q1
rename b3_q1 household_size
save "$nss/temp/temp_04_05_hh_size.dta", replace


use "$nss/raw/61st_round_2004_05/Data/Block 3 Part 2_Household Characteristics.dta", clear
rename *, lower
keep hhid-lvl nss nsc mlt wgt_* b3_q28 b3_q29 b3_q17 b3_q18

rename b3_q17 cooking_code
rename b3_q18 lighting_code

rename b3_q28 monthly_per_capita_exp_30
rename b3_q29 monthly_per_capita_exp_365

gen electric_cooking = 0
replace electric_cooking = 1 if cooking_code == "08"
gen electric_lighting = 0
replace electric_lighting = 1 if lighting_code == "5"
save "$nss/temp/temp_04_05_hh_chars_eleccodes.dta", replace



use "$nss/raw/61st_round_2004_05/Data/Block 6_Monthly consumption of fuel & light.dta" , clear
rename *, lower
keep hhid-lvl nss nsc mlt wgt_* b6_q1 b6_q5 b6_q6

gen fuellight_value = b6_q6 if b6_q1 == "359"
label var fuellight_value "tot expenditure on fuel and light (30 days)"

gen electricity_quantity = b6_q5 if b6_q1 == "342"
gen electricity_value = b6_q6 if b6_q1 == "342"

gen other_fuel_value = b6_q6 if b6_q1 == "353"


collapse(max) fuellight_value electricity_quantity electricity_value other_fuel_value, by (hhid vill_blk-lvl nss nsc mlt wgt_*)

assert fuellight >= electricity_value if fuellight !=. & electricity_value !=.
replace fuellight = 0 if fuellight == .
replace electricity_value = 0 if electricity_value == .
replace electricity_quant = 0 if electricity_quant == .
replace other_fuel_value = 0 if other_fuel_value == .

label var fuellight "Total spending on fuel and light (Rs)"
label var electricity_value "Total spending on electricity (Rs)"
label var electricity_quantity "Total quantity of electricity (std units)"
label var other_fuel_value "Total spending on other fuel (Rs)" 
gen implied_elec_price = electricity_val / electricity_quant 
label var implied_elec_price "Electricity price [Value (Rs) / quantity (units)]" 

save "$nss/temp/temp_04_05_fuelspending.dta", replace




use "$nss/raw/61st_round_2004_05/Data/Block 11_Expenditure for purchase and construction (including repair and maintenance) of durable goods for domestic use.dta", clear
rename *, lower
keep hhid-lvl nss nsc mlt wgt_* b11_q1 b11_q3

gen radio_yn = b11_q3 if b11_q1 == "561"
gen tv_yn = b11_q3 if b11_q1 == "562"

gen fan_yn = b11_q3 if b11_q1 == "590"
gen ac_yn = b11_q3 if b11_q1 == "591"
gen aircooler_yn = b11_q3 if b11_q1 == "592"
gen sewing_machine_yn = b11_q3 if b11_q1 == "594"
gen fridge_yn = b11_q3 if b11_q1 == "598"

destring radio tv fan ac_ aircooler sewing fridge, replace

collapse(max) radio tv fan ac_ aircooler sewing fridge, by(hhid vill_blk-lvl nss nsc mlt wgt_*)

foreach var of varlist radio - fridge {
  replace `var' = 0 if `var' == .
  replace `var' = 0 if `var' == 2
}  
save "$nss/temp/temp_04_05_elecdurables.dta", replace
 

** Merge 2004-05 datasets together
use "$nss/temp/temp_04_05_hh_chars.dta", clear
merge 1:1 hhid using "$nss/temp/temp_04_05_hh_chars_eleccodes.dta", keep (1 3) nogen
merge 1:1 hhid using "$nss/temp/temp_04_05_fuelspending.dta", keep(1 3) nogen
merge 1:1 hhid using "$nss/temp/temp_04_05_elecdurables.dta", keep(1 3) nogen
merge 1:1 hhid using "$nss/temp/temp_04_05_hh_size.dta", keep(1 3) nogen
foreach var of varlist electric_cooking - electricity_value other_fuel_value radio_yn - fridge {
  replace `var' = 0 if `var' == .
}
save "$nss/temp/temp_04_05_allelecdata.dta", replace

}

********************************************************************************
********************************************************************************

** Step 4: 2009-2010 wave
{
	
** Read in and clean the 2009-2010 data; grab covariates of interest
use "$nss/raw/66th_round_2009_10_type1/Data/Identification of Sample Household.dta", clear  
rename *, lower
drop dos dod ttc remark* filler
save "$nss/temp/temp_09_10_hh_chars.dta", replace

use "$nss/raw/66th_round_2009_10_type1/Data/Household Characteristics.dta", clear  
rename *, lower
keep hh_id - level hh_size cooking_code lighting_code mpce_urp mpce_mrp nss nsc mlt multiplier

rename hh_size household_size

gen electric_cooking = 0
replace electric_cooking = 1 if cooking_code == "08"
gen electric_lighting = 0
replace electric_lighting = 1 if lighting_code == "5"

la var mpce_mrp "monthly per capita consumption expenditure (mixed reference period)"
la var mpce_urp "monthly per capita consumption expenditure (uniform reference period)"
save "$nss/temp/temp_09_10_hh_chars_eleccodes.dta", replace



use "$nss/raw/66th_round_2009_10_type1/Data/Consumption of cereals, pulses, milk and milk products, sugar and salt during the last 30 days.dta" , clear
rename *, lower
keep hh_id-level item_code total_quantity total_value nss nsc mlt multiplier

gen fuellight_value = total_value if item_code == "359"
label var fuellight_value "tot expenditure on fuel and light (30 days)"

gen electricity_quantity = total_quantity if item_code == "342"
gen electricity_value = total_value if item_code == "342"


gen petrol_value = total_value if item_code == "353"
gen diesel_value = total_value if item_code == "354"


collapse(max) fuellight_value electricity_quantity electricity_value petrol_value diesel_value, by (hh_id fsu_serial-level nss nsc mlt multiplier)

assert fuellight >= electricity_value if fuellight !=. & electricity_value !=.
replace fuellight = 0 if fuellight == .
replace electricity_value = 0 if electricity_value == .
replace electricity_quant = 0 if electricity_quant == .

label var fuellight "Total spending on fuel and light (Rs)"
label var electricity_value "Total spending on electricity (Rs)"
label var electricity_quantity "Total quantity of electricity (std units)"
  
gen implied_elec_price = electricity_val / electricity_quant 
label var implied_elec_price "Electricity price [Value (Rs) / quantity (units)]" 

gen other_fuel_value = petrol_value + diesel_value
label var other_fuel_value "Total spending on other fuel (Rs)"
label var petrol_value "Total spending on petrol (Rs)"
label var diesel_value "Total spending on diesel (Rs)"

save "$nss/temp/temp_09_10_fuelspending.dta", replace


use "$nss/raw/66th_round_2009_10_type1/Data/Expenditure for purchase and construction (including repair and maintenance) of durable goods for domestic use.dta", clear
rename *, lower
keep hh_id-level item_code wether_possesses nss nsc mlt multiplier


gen radio_yn = wether if item == "560"
gen tv_yn = wether if item == "561"
gen vcr_yn = wether if item == "562"

gen fan_yn = wether if item == "580"
gen ac_yn = wether if item == "581"
gen sewing_machine_yn = wether if item == "583"
gen fridge_yn = wether if item == "587"
gen electric_heating_appliance_yn = wether if item == "590"

destring radio - electric_heat, replace

collapse(max) radio-electric_heat, by(hh_id fsu_serial-level nss nsc mlt multiplier)
la var ac_yn "ac or aircooler"

foreach var of varlist radio - electric_heat {
  replace `var' = 0 if `var' == .
  replace `var' = 0 if `var' == 2
}  
save "$nss/temp/temp_09_10_elecdurables.dta", replace

 

** Merge 2009-10 datasets together
use "$nss/temp/temp_09_10_hh_chars.dta", clear
merge 1:1 hh_id using "$nss/temp/temp_09_10_hh_chars_eleccodes.dta", keep (1 3) nogen
merge 1:1 hh_id using "$nss/temp/temp_09_10_fuelspending.dta", keep(1 3) nogen
merge 1:1 hh_id using "$nss/temp/temp_09_10_elecdurables.dta", keep(1 3) nogen

foreach var of varlist electric_cooking - electricity_value petrol_value diesel_value other_fuel_value radio_yn - electric_heating {
  destring `var', replace
  replace `var' = 0 if `var' == .
}

rename fsu_serial vill_blk_slno

save "$nss/temp/temp_09_10_allelecdata.dta", replace

}

********************************************************************************
********************************************************************************

** Step 5: General dataset cleanup to match across years
{
	
** 99-00
{
use "$nss/temp/temp_99_00_allelecdata.dta", clear
keep if sector == "1"

keep nss nsc household_size mult wgt_* vill_blk state district electric_cooking - elec_heating_appliance mpce30

tostring nss nsc, replace

foreach var of varlist record - electric_heating_appliance_no {
  replace `var' = 1 if `var' >=1 & `var' !=.
}

rename *_no *_yn

rename mpce30 monthly_per_capita_exp_30

gen year = "1999-2000"
save "$nss/temp/temp_99_00_selectvars.dta", replace
}

** 04-05
{
use "$nss/temp/temp_04_05_allelecdata.dta", clear
keep if sector == "1"

tostring nss nsc, replace
keep nss nsc mlt wgt_* household_size vill_blk state district electric_cooking - fridge_yn monthly_per_capita_exp_30


gen year = "2004-2005"
save "$nss/temp/temp_04_05_selectvars.dta", replace
}

** 09-10
{
use "$nss/temp/temp_09_10_allelecdata.dta", clear
keep if sector == "1"

tostring nss nsc, replace
keep nss nsc mlt multiplier household_size state district vill_blk electric_cooking - electric_heating_appliance_yn mpce_urp

rename mpce_urp monthly_per_capita_exp_30

gen year = "2009-2010"
save "$nss/temp/temp_09_10_selectvars.dta", replace
}

}

********************************************************************************
********************************************************************************

** Step 6: COMBINE
{
use "$nss/temp/temp_99_00_selectvars.dta", clear
append using "$nss/temp/temp_04_05_selectvars.dta"
append using "$nss/temp/temp_09_10_selectvars.dta"

drop record_player vcr washing elec_heating_appliance_value electric_heating_appliance_yn

replace ac_yn = 1 if aircooler_yn == 1 & ac_yn == 0

drop aircooler_yn

drop state_district

order state district year nss nsc mult mlt multiplier wgt_* vill_blk

la var radio_yn "Has radio? (1/0)"
la var ac_yn "Has AC? (1/0)"
la var fan_yn "Has fan? (1/0)"
la var sewing_machine_yn "Has sewing machine? (1/0)"
la var fridge_yn "Has fridge? (1/0)"
la var tv_yn "Has TV? (1/0)"

rename electric_cooking elec_cook_yn
rename electric_lighting elec_light_yn
la var elec_cook_yn "Main source of cooking electric? (1/0)"
la var elec_light_yn "Main source of lighting electric? (1/0)"

la var monthly_per_capita "Monthly per capita expenditure (30 day recall)"
la var year "NSS year"

rename vill_blk_slno fsu_serial_no

save "$nss/Final/nss_hhlevel_clean.dta", replace

}

********************************************************************************
********************************************************************************

** Step 7: Merge in multiplier files
{
use "$nss/Final/nss_hhlevel_clean.dta", clear
preserve
keep if year == "1999-2000"
tempfile nss_9900
save `nss_9900'

restore, preserve
keep if year == "2004-2005"

merge m:1 fsu_serial_no using "$nss/Final/mlt_61_1.dta", keep(3) nogen
tempfile nss_0405
save `nss_0405'

restore
keep if year == "2009-2010"

merge m:1 fsu_serial_no using "$nss/Final/mlt_66_1.dta", keep(3) nogen
tempfile nss_0910
save `nss_0910'

append using `nss_0405'
append using `nss_9900'


keep state - fridge_yn frame_population approx_present_pop nr_of_hamlet_groups

la var frame_pop "village population"
la var approx_present "approximate population present at village"
la var nr_of_hamlet_groups "number of habitations in village"

save "$nss/Final/nss_hhlevel_clean_withframepops.dta", replace

}

********************************************************************************
********************************************************************************

